{% extends "base.html" %} {% block content %} {% load custom_filter %}

<div class="row">
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h4 class="box-title">管理员密码<small> 修改您的管理员密码</small></h4>
            </div>
            <div class="box-body">
                <form class="form-horizontal" id="form_modify_pass">
                    <div class="form-group">
                        <label class="col-sm-1 control-label">原密码</label>
                        <div class="col-sm-4">
                            <div id="div_old_password">
                                <input type="password" class="form-control" name="old_password" id="input_old_password" placeholder="请输入当前密码" onblur="check_pass()">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">新密码</label>
                        <div class="col-sm-4">
                            <div id="div_new_password">
                                <input type="password" class="form-control" name="new_password" id="input_new_password" placeholder="请输入新密码" onblur="check_pass()">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">新密码</label>
                        <div class="col-sm-4">
                            <div id="div_verify_password">
                                <input type="password" class="form-control" name="verify_password" id="input_verify_password" placeholder="再次输入新密码" onblur="check_pass()">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="box-footer">
                <button type="button" class="btn" onclick="modify_pass(1)">重置管理员</button>
                <button type="button" class="btn btn-primary" onclick="modify_pass(0)">修改密码</button>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h4 class="box-title">网络选择<small> 用于创建防火墙规则</small></h4>
            </div>
            <div class="box-body">
                <form class="form-horizontal" id="form_select_nic">
                    <div class="form-group">
                        <label class="col-sm-1 control-label">内网卡</label>
                        <div class="col-sm-4">
                            <div id="div_select_nic">
                                <select class="form-control" name="select_nic" id="select_nic">
                                    {% for n in nic.nics %} {% if n.nic == nic.internal_nic %}
                                    <option value="{{ n.nic }}" selected>{{ n.nic }} - {{ n.address }}</option>
                                    {% else %}
                                    <option value="{{ n.nic }}">{{ n.nic }} - {{ n.address }}</option>
                                    {% endif %} {% endfor %}
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="box-footer">
                <button type="button" class="btn btn-primary" onclick="select_nic()">保存</button>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h4 class="box-title">配置备份<small> 通过文件备份和还原负载均衡配置</small></h4>
            </div>
            <div class="box-body">
                <button type="button" class="btn btn-primary" onclick="config_backup(1)">备份</button>
                <button type="button" class="btn btn-primary" onclick="import_config.click()">还原</button>
                <input type="file" style="display:none" name="import_config" id="import_config" onchange="config_backup(0)" />
            </div>
        </div>
    </div>
</div>


<script>
//init page
$('#menu_system_config').addClass('active')
$('#menu_service').removeClass('active')
$('#page_header').text('系统配置')
$('#page_header_descript').text('配置[ Nginx 负载均衡管理平台]')
$('#page_nav').text('系统配置')
$('#page_name').text('系统配置')

function check_pass() {
    var _old_pass = $('#input_old_password')[0]
    var _new_pass = $('#input_new_password')[0]
    var _verify_pass = $('#input_verify_password')[0]
    _return = false
    $(_new_pass.closest("div")).removeClass()
    $(_verify_pass.closest("div")).removeClass()
    if (_old_pass.value.length > 7 && _new_pass.value.length > 7 && _verify_pass.value.length > 7) {
        if (_new_pass.value == _verify_pass.value) {
            $(_new_pass.closest("div")).toggleClass('has-success')
            $(_verify_pass.closest("div")).toggleClass('has-success')
            _return = true
        } else {
            $(_new_pass.closest("div")).toggleClass('has-error')
            $(_verify_pass.closest("div")).toggleClass('has-error')
        }
    } else {
        $(_new_pass.closest("div")).toggleClass('has-error')
        $(_verify_pass.closest("div")).toggleClass('has-error')
    }
    return _return
}

function select_nic() {
    if (confirm("确认选择的是您的内网网卡？选择错误将有可能导致平台访问中断！选择网卡后系统将自动维护防火墙策略，22，8000 端口将会只对内网开放！外网将只开放负载均衡器的监听端口！")) {
        jQuery.ajax({
            type: 'post',
            url: '/settings/nic/',
            data: JSON.stringify($('#form_select_nic').serializeObject()),
            dataType: 'json',
            success: function(p) {
                if (p.flag == "Success") {
                    alert("修改成功！")
                    top.location = '/settings/'
                } else if (p.flag == "Error" && p.context == "VerifyFaild") {
                    alert('修改失败！')
                } else if (p.flag == "Error" && p.context == "AuthFailed") {
                    alert('认证失败！请重新登录！')
                    top.location = '/login/'
                } else {
                    alert('修改错误！其他错误：' + p.context)
                }
            },
            error: function(e) {
                alert('请求失败!')
            }
        })
    }
}

function config_backup(type) {
    if (type == 1) {
        if (confirm("此操作会将负载均衡器<全局配置>和<负载均衡配置>备份为文件以供还原，是否继续？")) {
            jQuery.ajax({
                type: 'post',
                url: '/settings/config/export/',
                dataType: 'json',
                success: function(p) {
                    if (p.flag == "Success") {
                        var blob = new Blob([JSON.stringify(p.context)], {
                            type: "text/plain;charset=utf-8"
                        });
                        saveAs(blob, "LazyBalancer.bak");
                        alert("备份成功！")
                        top.location = '/settings/'
                    } else if (p.flag == "Error" && p.context == "AuthFailed") {
                        alert('认证失败！请重新登录！')
                        top.location = '/login/'
                    } else {
                        alert('修改错误！其他错误：' + p.context)
                    }
                },
                error: function(e) {
                    alert('请求失败!')
                }
            })
        }
    } else {
        if (confirm("此操作会将负载均衡器<全局配置>和<负载均衡配置>通过您提供的配置文件覆盖，是否继续？")) {
            var reader = new FileReader()
            reader.readAsText($('#import_config')[0].files[0])
            reader.onload = function(e) {
                $.ajax({
                    type: 'post',
                    url: '/settings/config/import/',
                    data: this.result,
                    dataType: 'json',
                    success: function(p) {
                        console.log(p)
                        if (p.flag == "Success") {
                            alert("还原完成！")
                            top.location = '/settings/'
                        } else if (p.flag == "Error" && p.context == "AuthFailed") {
                            alert('认证失败！请重新登录！')
                            top.location = '/login/'
                        } else {
                            $('#import_config').val('')
                            alert('还原失败！错误：' + p.context)
                        }
                    },
                    error: function(e) {
                        $('#import_config').val('')
                        alert('请求失败!')
                    }
                })
            }
        } else {
            $('#import_config').val('')
        }
    }
}

function modify_pass(reset_admin) {
    if (reset_admin == 1) {
        if (confirm("确认要重置管理员？")) {
            jQuery.ajax({
                type: 'post',
                url: '/settings/admin/',
                data: '{"reset":1}',
                dataType: 'json',
                success: function(p) {
                    if (p.flag == "Success") {
                        alert("重置成功！请重新创建管理员账号！")
                        top.location = '/logout/'
                    } else {
                        alert('重置失败！其他错误：' + p.context)
                    }
                },
                error: function(e) {
                    alert('请求失败!')
                }
            })
        }
    } else {
        if (check_pass()) {
            if (confirm("确认要修改密码？")) {
                jQuery.ajax({
                    type: 'post',
                    url: '/settings/password/',
                    data: JSON.stringify($('#form_modify_pass').serializeObject()),
                    dataType: 'json',
                    success: function(p) {
                        if (p.flag == "Success") {
                            alert("修改成功！请重新登录！")
                            top.location = '/logout/'
                        } else if (p.flag == "Error" && p.context == "VerifyFaild") {
                            alert('修改失败！密码验证失败！')
                        } else if (p.flag == "Error" && p.context == "AuthFailed") {
                            alert('认证失败！请重新登录！')
                            top.location = '/login/'
                        } else {
                            alert('修改错误！其他错误：' + p.context)
                        }
                    },
                    error: function(e) {
                        alert('请求失败!')
                    }
                })
            }
        } else {
            alert('密码输入有误！')
        }
    }
}
</script>
{% endblock %}
